package de.szut.ita13.sommerfeld.audio;

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import de.szut.ita13.sommerfeld.gui_soundboard.GUI;

public class ButtonListener implements ActionListener {

	public static FileDialog filedialog;
	
	public ButtonListener() {
		
		filedialog = new FileDialog(new Frame(),"Select a File");
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		AudioButton clickedAudioButton = (AudioButton) arg0.getSource();
		if(!clickedAudioButton.hasAudioFile() || (clickedAudioButton.hasAudioFile() && clickedAudioButton.isEditable())) {
			filedialog.setVisible(true);
			if(filedialog.getFile() != null) {
				clickedAudioButton.resetCounter();
				clickedAudioButton.setAudioFile(new File(filedialog.getDirectory() + "/" + filedialog.getFile()));
				clickedAudioButton.setName(clickedAudioButton.getName());
				clickedAudioButton.setLabel(clickedAudioButton.getName());
				clickedAudioButton.setBackground(Color.GREEN);
			}
		} else if((clickedAudioButton.hasAudioFile() && !clickedAudioButton.isEditable() && clickedAudioButton.isRemovable())) {
			clickedAudioButton.removeAudioFile();
			clickedAudioButton.setBackground(Color.RED);
			if(!AudioButton.hasAudioButtonWithAudioFile(GUI.audioButtons)) {
				GUI.REMOVABLE = false;
				GUI.removeAudioButton.setForeground(Color.RED);
			}

		} else {
			clickedAudioButton.setBackground(Color.BLUE);
			clickedAudioButton.play(clickedAudioButton.getAudioFile());
			clickedAudioButton.incrementCounter();
			clickedAudioButton.setBackground(Color.GREEN);
		}
	}
}
